package com.useeinfo.oa.modules.threadUtil;

import com.useeinfo.oa.modules.base.biz.*;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.FacilityImport;
import com.useeinfo.oa.modules.base.enums.MessageTypeEnum;
import com.useeinfo.oa.modules.base.thread.task.MessageSendTask;
import com.useeinfo.oa.modules.base.vo.MessageVO;
import com.useeinfo.oa.modules.inspection.biz.InspectionOrderBiz;
import com.useeinfo.oa.modules.inspection.biz.InspectionTechnologyBiz;
import com.useeinfo.oa.modules.inspection.entity.InspectionExecuteTime;
import com.useeinfo.oa.modules.inspection.entity.InspectionTechnology;
import com.useeinfo.oa.modules.inspection.entity.PlannerInspection;
import com.useeinfo.oa.modules.inspection.thread.task.InsOrderTask;
import com.useeinfo.oa.modules.inspection.thread.task.InspectionPlanTask;
import com.useeinfo.oa.modules.logger.biz.FacilityLogBiz;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderBiz;
import com.useeinfo.oa.modules.maintaining.biz.MaintainTechnologyBiz;
import com.useeinfo.oa.modules.maintaining.biz.PeriodInstallBiz;
import com.useeinfo.oa.modules.maintaining.entity.PlanExecuteTime;
import com.useeinfo.oa.modules.base.entity.PlanMessageSend;
import com.useeinfo.oa.modules.maintaining.entity.PlannerOrder;
import com.useeinfo.oa.modules.maintaining.thread.task.OrderTask;
import com.useeinfo.oa.modules.maintaining.thread.task.PlanTask;
import com.useeinfo.oa.modules.security.biz.OrganizationBiz;
import com.useeinfo.oa.modules.threadUtil.message.PushDeviceMessage;
import com.useeinfo.oa.modules.threadUtil.thread.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程工具
 */
public class ThreadUtils {

    private final static Logger logger = LoggerFactory.getLogger(ThreadUtils.class);

    private static ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();

    /**
     * 上传导入文件
     *
     * @param facilityImportBiz
     * @param facilityBiz
     * @param facilityLogBiz
     * @param facilityImport
     */
    public static void importFacilityThread(FacilityImportBiz facilityImportBiz, FacilityBiz facilityBiz, FacilityLogBiz facilityLogBiz, FacilityGroupBiz facilityGroupBiz, FacilityCatBiz facilityCatBiz, AreaBiz areaBiz, OrganizationBiz organizationBiz, FacilityImport facilityImport) {
        try {
            createPoll();
            singleThreadExecutor.execute(new FacilityThread(facilityImportBiz, facilityBiz, facilityLogBiz, facilityGroupBiz, facilityCatBiz, areaBiz, organizationBiz, facilityImport));
        } catch (Exception e) {
            logger.info("{}", e);
        }
    }

    /**
     * 导入用户
     *
     * @param personnelBiz
     * @param wb           工作簿
     * @param enterprise   企业
     */
    public static void importUserThread(PersonnelBiz personnelBiz, Workbook wb, Enterprise enterprise) {
        try {
            createPoll();
            singleThreadExecutor.execute(new ImportUserThread(personnelBiz, wb, enterprise));
        } catch (Exception e) {
            logger.info("{}", e);
        }
    }

    /**
     * 导入数据字典
     */
    public static void importDictThread(DictInfoBiz dictInfoBiz,Workbook wb,Enterprise enterprise){
        try{
            createPoll();
            singleThreadExecutor.execute(new ImportDictThread(dictInfoBiz, wb, enterprise));
        }catch (Exception e){
            logger.info("{}",e);
        }

    }


    /**
     * 导入维保工艺
     */
    public static void importMaintainTechnologyThread(MaintainTechnologyBiz maintainTechnologyBiz, Workbook wb, Enterprise enterprise){
        try {
            createPoll();
            singleThreadExecutor.execute(new MaintainTechnologyThread(maintainTechnologyBiz, wb, enterprise));
        } catch (Exception e) {
            logger.info("{}", e);
        }
    }

    /**
     * 导入巡检工艺
     */
    public static void importInspectionTechnologyThread(InspectionTechnologyBiz inspectionTechnologyBiz,Workbook wb, Enterprise enterprise){
        try {
            createPoll();
            singleThreadExecutor.execute(new InspectionTechnologyThread(inspectionTechnologyBiz, wb, enterprise));
        } catch (Exception e) {
            logger.info("{}", e);
        }
    }


    /**
     * 发送设备消息
     *
     * @param messageVOS 发送的设备
     * @param message    消息
     */
    public static void sendDeviceMessages(List<MessageVO> messageVOS, String message, MessageTypeEnum messageTypeEnum, MessageBiz messageBiz,String enterpriseId,String code) {
        try {
            createPoll();
            singleThreadExecutor.execute(new PushDeviceMessage(messageVOS, message, messageTypeEnum, messageBiz,enterpriseId,code));
        } catch (Exception e) {
            logger.info("{}", e);
        }
    }

    /**
     * 计划任务
     *
     * @param inspectionOrderBiz
     * @param inspectionExecuteTimes
     */
    public static void inspectionExecuteTimes(InspectionOrderBiz inspectionOrderBiz, List<InspectionExecuteTime> inspectionExecuteTimes) {
        try {
            createPoll();
            logger.info("查询出需要生成的巡检工单数量为："+inspectionExecuteTimes.size());
            InspectionPlanTask inspectionPlanTask = new InspectionPlanTask(inspectionOrderBiz, inspectionExecuteTimes);
            singleThreadExecutor.execute(inspectionPlanTask);
        } catch (Exception e) {
            logger.info("{}", e);
        }
    }

    /**
     * 计划任务
     *
     * @param maintainOrderBiz
     * @param planExecuteTimes
     */
    public static void planExecuteTimes(MaintainOrderBiz maintainOrderBiz, List<PlanExecuteTime> planExecuteTimes) {
        try {
            createPoll();
            logger.info("查询出需要生成的维保工单数量为："+planExecuteTimes.size());
            PlanTask planTask = new PlanTask(maintainOrderBiz, planExecuteTimes);
            singleThreadExecutor.execute(planTask);
        } catch (Exception e) {
            logger.info("{}", e);
        }
    }


    public static void messageSendExecute(MessageBiz messageBiz, List<PlanMessageSend> planMessageSends){
        try{
            createPoll();
            MessageSendTask messageSendTask = new MessageSendTask(planMessageSends,messageBiz);
            singleThreadExecutor.execute(messageSendTask);
        }catch (Exception e){
            logger.info("{}",e);
        }
    }


    /**
     * 生成工单
     *
     * @param periodInstallBiz
     * @param plannerOrder
     */
    public static void periodExecuteOrder(PeriodInstallBiz periodInstallBiz, PlannerOrder plannerOrder) {
        try {
            createPoll();
            OrderTask orderTask = new OrderTask(periodInstallBiz, plannerOrder);
            singleThreadExecutor.execute(orderTask);
        } catch (Exception e) {
            logger.info("{}", e);
        }
    }

    /**
     * 生成工单
     *
     * @param periodInstallBiz
     * @param inspection
     */
    public static void InsExecuteOrder(PeriodInstallBiz periodInstallBiz, PlannerInspection inspection) {
        try {
            createPoll();
            InsOrderTask insOrderTask = new InsOrderTask(periodInstallBiz, inspection);
            singleThreadExecutor.execute(insOrderTask);
        } catch (Exception e) {
            logger.info("{}", e);
        }
    }


    /**
     * 判断线程池是否存在，不存在创建
     */
    private static void createPoll() {
        if (singleThreadExecutor.isShutdown()) {
            singleThreadExecutor = Executors.newSingleThreadExecutor();
        }
    }


}
